<?php  
/*
$Id$
*/


if (!defined('BASEPATH')) exit('No direct script access allowed');

class Ajax extends Controller {

	function Ajax()
	{
		parent::Controller();
		$this->template['module'] = "album";
		$this->load->model("album_model", "album");
		if(!isset($this->album->album_settings))
		{
			echo __("This module is not yet set up.", "album")  . "<br />";
			echo anchor('admin/album/settings', __("Go here to set it up", "album"));
			exit();
		}
	}
	
	
	function index()
	{
		$this->lisitra();
	}

	//lisitry ny tahirintsary
	function lisitra( $start = 0)
	{
		$per_page = 20;
		$params['start'] = $start;
		$params['per_page'] = $per_page;
		$params['order_by'] = "id DESC";
		$this->template['rows'] = $this->album->get_albums($params);
		
		$this->template['title'] = __("Album list", "album");
		$config['uri_segment'] = 3;
		$config['first_link'] = __('First', 'album');
		$config['last_link'] = __('Last', 'album');
		$config['base_url'] = base_url() . 'album/lisitra';
		$config['total_rows'] = $this->album->get_total_albums($params);
		$config['per_page'] = $per_page; 

		$this->load->library('pagination');
		
		$this->pagination->initialize($config); 

		$this->template['pager'] = $this->pagination->create_links();		
		$this->template['start'] = $start;		
		$this->template['total'] = $config['total_rows'];		
		$this->template['per_page'] = $config['per_page'];		
	
		
		$this->layout->load($this->template, 'album/index');
		
	}
	
	function asehoy( $aid = null, $start = 0)
	{
		if (is_null($aid))
		{
			echo __("You didn't choose any album", "album");
			return;
		}
		
		$per_page = 24;
		
		$params['start'] = $start;
		$params['limit'] = $per_page;
		$params['order_by'] = "album_images.id DESC";
		$params['where'] = array('album_images.aid' => $aid);
			
		$sary = $this->album->get_images($params);
		$this->template['sary'] = $sary;

		$this->load->library('pagination');
		$config['uri_segment'] = 5;
		$config['first_link'] = __('First', "album");
		$config['last_link'] = __('Last', "album");
		$config['base_url'] = base_url() . 'album/ajax/asehoy/' . $aid;
		$config['total_rows'] = $this->album->get_total_images($params);
		$config['per_page'] = $per_page; 

		$this->pagination->initialize($config); 

		$this->template['pager'] = $this->pagination->create_links();		

		$this->load->view('album/ajax/asehoy', $this->template);
		
	}
	
	function user($username = null, $start = 0)
	{
		if(is_null($username))
		{
			redirect ('album');
			return;
		}
		$per_page = 20;
		
		
		$this->load->library('pagination');
		
		$params['start'] = $start;
		$params['per_page'] = $per_page;
		$params['order_by'] = "id DESC";
		$params['where'] = array('a.username' => $username);
		$this->template['rows'] = $this->album->get_albums($params);
		
		$this->template['title'] = sprintf(__("Album that belong to %s", "album"), $username);
		$config['uri_segment'] = 4;
		$config['first_link'] = __('First', 'album');
		$config['last_link'] = __('Last', 'album');
		$config['base_url'] = base_url() . 'album/user/' . $username;
		$config['total_rows'] = $this->album->get_total_albums($params);
		
		$config['per_page'] = $per_page; 

		$this->pagination->initialize($config); 

		$this->template['pager'] = $this->pagination->create_links();		
		$this->template['start'] = $start;		
		$this->template['total'] = $config['total_rows'];		
		$this->template['per_page'] = $config['per_page'];		
	
		
		$this->layout->load($this->template, 'album/index');
		
	}
	
	function tag($tag = null, $start = 0)
	{
		if(is_null($tag))
		{
		
			$this->template['title'] = __("Tag list", "album");
			
			$this->template['rows'] = $this->album->get_tags();
			
			$this->layout->load($this->template, 'tags');
			
			return;
		}
		
		$per_page = 20;
		
		
		$this->load->library('pagination');
		
		$params['start'] = $start;
		$params['per_page'] = $per_page;
		$params['order_by'] = "id DESC";
		$this->template['rows'] = $this->album->get_album_by_tag($tag, $start, $per_page);
		
		$this->template['title'] = __("Album with the spicified tag", "album");
		$config['uri_segment'] = 4;
		$config['first_link'] = __('First', 'album');
		$config['last_link'] = __('Last', 'album');
		$config['base_url'] = base_url() . 'album/tag/' . $tag;
		$config['total_rows'] = $this->album->get_total_album_by_tag($tag);
		$config['per_page'] = $per_page; 

		$this->pagination->initialize($config); 

		$this->template['pager'] = $this->pagination->create_links();		
		$this->template['start'] = $start;		
		$this->template['total'] = $config['total_rows'];		
		$this->template['per_page'] = $config['per_page'];		
	
		
		$this->layout->load($this->template, 'album/index');
	
		
	}
	function ampidiro($aid = null)
	{
		$this->template['title'] = __("Create an album", $this->template['module']);
		$this->user->require_login();
	
		$this->template['tags'] = $this->album->get_tags(array('limit' => 40, 'order_by' => 'cnt DESC' ));
		
		if ($this->input->post('submit'))
		{
			if (!$this->input->post('title'))
			{
				$this->template['error'][] = __("Title is required.", "album");
				$this->template['row'] = $_POST;
				$this->layout->load($this->template, 'ampidiro');
				return;
			}
			
			if ($aid = $this->input->post('aid'))
			{
				$row = $this->album->get_album(array('where' => array('a.aid' => $aid)));
				if(($this->user->level['album'] < LEVEL_EDIT) && ($this->user->username != $row['username']))
				{
					$this->template['error'][] = __("You cannot edit this album", "album") ;
					$this->template['row'] = $_POST;
					$this->layout->load($this->template, 'ampidiro');
					return;
				}

				$aid = $this->album->save_album();
				$this->session->set_flashdata('notification', __("Album updated", "album"));
				
				redirect("album/asehoy/" . $aid);
				
			}
			else
			{

				$aid = $this->album->save_album();
				$this->session->set_flashdata('notification', __("Album saved", "album"));
				redirect("album/asehoy/" . $aid);
				
			}
		}
		
		

		$row = array();
		
		if ($last_post = $this->session->userdata("last_post"))
		{
			$this->template['row'] = $last_post;
			$this->session->set_userdata("last_post", array());
		}
		
		if (!is_null($aid))
		{
			$this->template['title'] = __("Modify the album", "album");
			$row = $this->album->get_album(array('limit' => null, 'where' => array('a.aid' => $aid)));
			if($row['username'] != $this->user->username)
			{
				//check only if not the submitter
				$this->user->check_level($this->template['module'], LEVEL_EDIT);
			}
			$this->template['row'] = $row;
			$this->template['sary'] = $this->album->get_images(array('where' => array('album_images.aid' => $row['aid'])));
		}
	
		$this->layout->load($this->template, 'album/ampidiro');
	
	}
	
	function fafao( $aid , $js = 0)
	{
		$this->user->require_login();
		$this->template['title'] = __("Delete an album", "album");
		if (is_null($aid))
		{
			$this->session->set_flashdata("notification", __("Please choose an album.", "album"));
			
			redirect("album/user/" . $this->user->username);
			return;
		}
		
		if ($this->album->get_images(array('where' => array('album_images.aid' => $aid))))
		{
			$this->session->set_flashdata("notification", __("That album is not empty, please delete all images in it.", "album"));
			
			redirect("album/ampidiro/" . $aid);
		}
		
		
		if ($js == 0)
		{
			$this->template['row'] = $this->album->get_album(array('where' => array('aid' => $aid)));
			$this->layout->load($this->template, "delete");
			return;
		}
		
		$row = $this->album->get_album(array('where' => array('a.aid' => $aid)));
		
		if($row['username'] != $this->user->username)
		{
			//check only if not the submitter
			$this->user->check_level($this->template['module'], LEVEL_DEL);
		}
		
		if ($this->album->delete($aid))
		{
			$this->session->set_flashdata("notification", __("Album deleted"));
			redirect("album/user/" . $this->user->username);
		}
		else
		{
			$this->session->set_flashdata("notification", __("Unknown error.", "album"));
			
			redirect("album/user/" . $this->user->username);
		}
	}

	function hevitra($sid = null, $start = 0)
	{

		$per_page = 10;
		if (is_null($sid))
		{
			$this->template['title'] = __("Comment error", "album");
			$this->template['message'] = __("You did not choose a photo.", "album");
			$this->load->view('error', $this->template);
			return;
			
		}
		$total = $this->album->get_total_comments(array('where' => array('album_image_comments.sid' => $sid)));
		$limit = 10;
		if($total - $start < 10) $limit = $total - $start;
		if ($rows = $this->album->get_comments(array('start' => $start, 'limit' => $limit, 'order_by' => 'album_image_comments.date DESC', 'where' => array('album_image_comments.sid' => $sid ))))
		{
			$jsrow = array();
			$i = $total;
			foreach ($rows as $row)
			{
				$a = array();
				$a['n'] = $i - $start;
				$a['id'] = $row['id'];
				$a['md5username'] = md5($row['username']);
				$a['comment'] = $row['comment'];
				$a['username'] = $row['username'];
				$a['date'] = date("d/m/Y H:i:s", $row['date']);
				$a['start'] = $start + $per_page;
				$jsrow[] = $a;
				$i--;
			}
			echo json_encode($jsrow);
			exit();
		}
		
	}
	
}